package com.tetra.arena.mysql;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.tetra.arena.core.ArenaSystems;

public class MySQLc extends SQLMainC {
	public MySQLc(ArenaSystems instance) throws ClassNotFoundException,
			SQLException {
		super(instance);
		plugin = instance;
		getConnection();
		setup();
	}

	ArenaSystems plugin;

	final protected synchronized void getConnection()
			throws ClassNotFoundException, SQLException {
		String host = plugin.getmServer();
		String username = plugin.getUsrn();
		String password = plugin.getPswd();
		String databaseName = plugin.getDBName();
		String port = plugin.getPort();
		Class.forName("com.mysql.jdbc.Driver");
		plugin.getLogger().info("MySQL driver loaded");
		conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port
				+ "/" + databaseName, username, password);
		plugin.getLogger().info("Connected to Database");
	}

	final protected synchronized void setup() throws SQLException {
		Statement st = null;
		try {
			st = conn.createStatement();
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `players` ("
					+ "`player_id` INTEGER NOT NULL AUTO_INCREMENT,"
					+ "`username` varchar(255) NOT NULL,"
					+ "`team_id` INTEGER DEFAULT NULL,"
					+ "`party_id` int(11) DEFAULT NULL,"
					+ "PRIMARY KEY (`player_id`),"
					+ "UNIQUE KEY `username` (`username`)"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `matches` ("
					+ "`match_id` INTEGER NOT NULL AUTO_INCREMENT,"
					+ "`team1_id` INTEGER NOT NULL,"
					+ "`team2_id` INTEGER NOT NULL,"
					+ "`victor` INTEGER NOT NULL," + "PRIMARY KEY (`match_id`)"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `teams` ("
					+ " `id` INTEGER NOT NULL AUTO_INCREMENT,"
					+ "`team_name` varchar(255) NOT NULL,"
					+ "`rating` INTEGER DEFAULT NULL,"
					+ "`win_count` INTEGER DEFAULT NULL,"
					+ "`loss_count` INTEGER DEFAULT NULL,"
					+ "`owner` INTEGER NOT NULL,"
					+ "`open` tinyint(1) NOT NULL DEFAULT '0',"
					+ "PRIMARY KEY (`id`),"
					+ "UNIQUE KEY `team_name` (`team_name`)"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `invites` ("
					+ "`invite_id` int(11) NOT NULL AUTO_INCREMENT,"
					+ "`player_id` int(11) NOT NULL,"
					+ "`team_id` int(11) NOT NULL,"
					+ "PRIMARY KEY (`invite_id`)"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `party_invites` ("
					+ "`invite_id` int(11) NOT NULL AUTO_INCREMENT,"
					+ "`player_id` int(11) NOT NULL,"
					+ "`party_id` int(11) NOT NULL,"
					+ "PRIMARY KEY (`invite_id`)"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			st.executeUpdate("CREATE TABLE IF NOT EXISTS `parties` ("
					+ "`party_id` int(11) NOT NULL AUTO_INCREMENT,"
					+ "`total_players` int(11) DEFAULT '0',"
					+ "`team_id` int(11) NOT NULL,"
					+ "`leader_id` int(11) NOT NULL,"
					+ "PRIMARY KEY (`party_id`),"
					+ "UNIQUE KEY `leader_id` (`leader_id`) USING BTREE"
					+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
			
		} finally {
			if (st != null) {
				try {
					st.close();
				} catch (SQLException ex) {
				}
			}
		}
	}

}
